<?php
ob_start();
echo "<div class='switcher'>";
echo __("Switch to ").$html->link("Reviews",array("plugin"=>null,"controller"=>"reviews","action"=>"index"));
if(cr('ReviewConfig.allow_comment') != '1')
{
	echo "<br/><br/>";
	echo sprintf(__('Review comments are disabled. To enable them go to %s page.'),$html->link(__('review settings'),'/admin/site_config/setting/Review'));
}
echo "</div><br/>";
$switchToLinks = ob_get_clean();

if(cr('ReviewConfig.allow_comment') != '1')
{ 
	echo $switchToLinks;
} else {
	echo $this->element('admin_search_filter');
	
	echo $switchToLinks;
	
	if(!empty($reviewComments))
	{	
		$index->setData($reviewComments,'ReviewComment');
		$index->showFields(array('id',array('created'=>'Date Added'),'body',array('review_id'=>'Review Title'),array('user_id'=>'Posted By')));
		$index->sortableFields(array('id','body'));
		
		$index->setAction(__('View'),'/admin/review_comments/view/:ReviewComment.id');
		$index->setAction(__('Edit'),'/admin/review_comments/edit/:ReviewComment.id');
		$index->setAction(__('Delete'),'/admin/review_comments/delete/:ReviewComment.id',$this->helpers,__('Are you sure you want to delete this comment ?'));
		$index->setAction(null,null,$this->helpers,false,true,'publish_unpublish');
		
		$index->addColumn('body','formatCommentBody');
		$index->addColumn('user_id','setPosterName');
		$index->addColumn('review_id','setReviewTitle');	
		$index->addColumn('created','showFormatedDate');
		
		$index->addButton(__("Delete Selected"),array("action"=>"bulkDelete",'admin'=>true));
		$index->addButton(__("Unpublish Selected"),array("action"=>"bulkUnpublish",'admin'=>true));
		$index->addButton(__("Publish Selected"),array("action"=>"bulkPublish",'admin'=>true));
		
		function showFormatedDate($record)
		{
			return date('Y-m-d',strtotime($record['ReviewComment']['created']));
		}
		function formatCommentBody($record)
		{
			uses("Flay");
			return Flay::fragment($record['ReviewComment']['body'],20);
		}
		
		function setPosterName($record)
		{
			return $record['ReviewComment']['poster']['display_name'];
		}
		
		function setReviewTitle($record)
		{
			App::import('Helper','Html');
			$Html = new HtmlHelper;
			uses("Flay");
			$title = Flay::fragment($record['Review']['title'],20);
			return $Html->link($title,'/admin/reviews/view/'.$record['Review']['id']);
		}
		
		function publish_unpublish($model,$record)
		{
			if($record[$model]['status'] == ReviewComment::PUBLISH)
			{
				$action['title'] = __('Unpublish');
				$action['url'] = '/admin/review_comments/changeStatus/unpublish/'.$record[$model]['id'];
			}else
			{
				$action['title'] = __('Publish');
				$action['url'] = '/admin/review_comments/changeStatus/publish/'.$record[$model]['id'];
			}
			return $action;
		}
		$index->setPaginateSeparator(' ');
		echo $index->display();
	}else
	{
		echo "<h4>".__('No review comments found!')."</h4>";
	}
	?>
<?php }?>